#!/bin/sh

source scripts/common.sh

####################################################################################################

TIFF_VERSION=4.0.0beta5
TIFF_BASENAME="tiff"
TIFF_ARCHIVE="${TIFF_BASENAME}-${TIFF_VERSION}.tar.gz"
TIFF_URL="http://download.osgeo.org/libtiff/${TIFF_ARCHIVE}"
TIFF_DIR="${TIFF_BASENAME}-${TIFF_VERSION}"
TIFF_PREFIX="${THIRD_PARTY_PREFIX}/${TIFF_BASENAME}"
TIFF_LIB=libtiff.a

####################################################################################################

function build_tiff()
{

target=$1

mkdir -p ${THIRD_PARTY_BUILD} || exit 1
pushd ${THIRD_PARTY_BUILD} || exit 1

report ""
report "Removing old build from '${TIFF_DIR}'..."
rm -fr $TIFF_DIR
rm -fr $TIFF_PREFIX/${target}

report "Preparing ${target} '${TIFF_BASENAME}'..."
fetch $TIFF_URL $TIFF_ARCHIVE

report "Configuring ${target} '${TIFF_BASENAME}'..."
report ""

pushd ${TIFF_DIR} || exit 1
mkdir -p ${target} || exit 1

./configure \
		CXXFLAGS="${SYSROOT} -arch ${target}" \
		CFLAGS="${SYSROOT} -arch ${target}" \
		LDFLAGS="-Wl,-syslibroot,${DEVSDK} -arch ${target}" \
		--prefix=${TIFF_PREFIX}/${target} \
        --target=${target}-apple-darwin \
        --with-apple-opengl-framework \
        --with-jpeg-include-dir=${THIRD_PARTY_PREFIX}/jpeg/universal/include \
        --with-jpeg-lib-dir=${THIRD_PARTY_PREFIX}/jpeg/universal/lib \
        --disable-shared \
        --enable-static \
        --quiet \
        --silent

if test $? -ne 0; then
        echo "configure failed"
        exit 1
fi

report "Building ${target} '${TIFF_BASENAME}'..."
nice make -j4 || exit 1
nice make install || exit 1

report "Built ${target} '${TIFF_BASENAME}'"
report ""

popd
popd

}

####################################################################################################

build_for_snow_leopard
for target in 'i686' 'x86_64' 'ppc'
do
	build_tiff $target
done

create_universal_lib ${TIFF_PREFIX} ${TIFF_LIB}
create_intel_lib ${TIFF_PREFIX} ${TIFF_LIB}
create_powerpc_lib ${TIFF_PREFIX} ${TIFF_LIB}

####################################################################################################

